package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    //按条件分页查询
    @Override
    public PageResult findPage(QueryPageBean pageBean) {
        Integer currentPage = pageBean.getCurrentPage();
        Integer pageSize = pageBean.getPageSize();

        PageHelper.startPage(currentPage,pageSize);
        //调用dao中的按条件查询方法，返回结果为查询到的角色集合
        List<Role> roleList = roleDao.findByCondition(pageBean.getQueryString());

        PageInfo<Role> pageInfo = new PageInfo<>(roleList);

        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    //查询角色基础信息用于回显数据
    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    //查询角色对应的菜单Id数组和权限Id数组用于回显数据
    @Override
    public Map<String, Integer[]> findMenuIdsAndPermissionIdsByRoleId(Integer roleId) {
        Integer[] menuIds = roleDao.findMenuIdsByRoleId(roleId);
        Integer[] permissionIds = roleDao.findPermissionIdsByRoleId(roleId);

        Map<String, Integer[]> dataMap = new HashMap<>();
        dataMap.put("menuIds",menuIds);
        dataMap.put("permissionIds",permissionIds);
        return dataMap;
    }

    //查询所有角色
    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    //添加角色
    @Override
    public void addRole(Role role, Integer[] menuIds, Integer[] permissionIds) {
        roleDao.add(role);

        Integer roleId = role.getId();

        setRoleAndMenuRelation(roleId, menuIds);
        setRoleAndPermissionRelation(roleId, permissionIds);
    }

    //编辑保存角色信息
    @Override
    public void edit(Role role, Integer[] menuIds, Integer[] permissionIds) {
        Integer roleId = role.getId();

        //删除原有的关联关系
        deleteRoleAndMenuRelation(roleId);
        deleteRoleAndPermissionRelation(roleId);

        //更新角色信息
        roleDao.edit(role);
        //建立新的关联关系
        setRoleAndMenuRelation(roleId, menuIds);
        setRoleAndPermissionRelation(roleId, permissionIds);
    }


    //删除
    @Override
    public void deleteById(Integer roleId) {
        Integer[] menuIds = roleDao.findMenuIdsByRoleId(roleId);
        Integer[] permissionIds = roleDao.findPermissionIdsByRoleId(roleId);
        if (menuIds != null && menuIds.length > 0){
            throw new RuntimeException();
        }
        if (permissionIds != null && permissionIds.length >0){
            throw new RuntimeException();
        }
        roleDao.deleteById(roleId);
    }

    //删除角色和菜单关系表数据
    private void deleteRoleAndMenuRelation(Integer roleId) {
        roleDao.deleteRoleAndMenuRelation(roleId);
    }

    //删除角色和权限关系表数据
    private void deleteRoleAndPermissionRelation(Integer roleId) {
        roleDao.deleteRoleAndPermissionRelation(roleId);
    }


    //设置角色和菜单表的关联关系
    private void setRoleAndMenuRelation(Integer roleId, Integer[] menuIds) {
        if (menuIds != null && menuIds.length > 0){
            for (Integer menuId : menuIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("roleId", roleId);
                map.put("menuId", menuId);
                roleDao.setRoleAndMenu(map);
            }
        }
    }

    //设置角色和权限表的关联关系
    private void setRoleAndPermissionRelation(Integer roleId, Integer[] permissionIds) {
        if (permissionIds != null && permissionIds.length > 0){
            for (Integer permissionId : permissionIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("roleId", roleId);
                map.put("permissionId", permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
    }

}
